package com.ktjy.controller;

import com.ktjy.entity.Book;
import com.ktjy.entity.Page;
import com.ktjy.entity.R;
import com.ktjy.entity.UserAco;
import com.ktjy.service.BookService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@CrossOrigin("*")
@Controller
@RequestMapping("/main")
public class BookController {
    @Autowired
    private BookService bookService;

    @ResponseBody
    @GetMapping("/bookList")
    public R selectBookList(Model model,@RequestParam Integer pageNo,@RequestParam Integer pageSize,@RequestParam String bName){
         Book book=new Book();
        Page pageVo=bookService.findPageVo(book,pageNo,pageSize);
        return R.ok(pageVo);
    }

    @RequestMapping("/add")
    public String add(Model model){
        return "add";
    }

    @ResponseBody
    @PostMapping("/insert")
    public R insert(@RequestBody Book book){
        int count = bookService.insert(book);
        return R.ok(count);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public R delete(Book book){
        int count = bookService.delete(book);
        return R.ok(count);
    }

    @ResponseBody
    @RequestMapping("/selectByIdBook")
    public R selectByIdBook(Model model,Book book){
       Book b = bookService.selectByIdBook(book);
       return R.ok(b);
    }

    @ResponseBody
    @RequestMapping("/update")
    public R updateBook(Book book){
        int i = bookService.updateByIdBook(book);
        return R.ok();
    }
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @ResponseBody
    @PostMapping("/go")
    public R go(@RequestBody UserAco userAco, HttpSession session){
        UserAco user;
        if(userAco.getUsername()!=null && !userAco.getUsername().isEmpty()){
            user=bookService.selectAccount(userAco);
            if(user==null){
                return R.fail();
            }
            if(user.getPassword().equals(userAco.getPassword())){
                return R.ok(user);
            }else {
                return R.fail();
            }
        }
        return R.fail();
    }
}
